package java学习;

import java.util.Random;
import java.util.Scanner;
import java.util.logging.LoggingPermission;

/**
 * **************
 * 项目名称:  <br/>
 * 文件名称:  <br/>
 * 文件描述: 线程停止模拟。 <br/>
 * 文件创建：刘世锦 <br/>
 * 创建时间: 2022/4/6 <br/>
 *
 * @version v1.0 <br/>
 * @update [序号][日期YYYY-MM-DD][更改人姓名][变更描述]<br/>
 * *************
 */
public class ThreadHomework1 {
    public static void main(String[] args) {
//        AThread a = new AThread();
//        a.start();
//        BThread bThread = new BThread(a);
//        bThread.start();
        CThread cThread = new CThread();
        new Thread(cThread).start();
        new Thread(cThread).start();
        new Thread(cThread).start();

        byte X = (byte) 1000;
        System.out.println(X);
    }


}

class AThread extends Thread{

    private boolean loop = true;

    public void setLoop(boolean loop) {
        this.loop = loop;
    }

    @Override
    public void run() {
        while (loop){

            System.out.println((int) (Math.random() * 100));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        System.out.println("A线程结束");
    }
}


class BThread extends Thread{

    private Scanner scanner = new Scanner(System.in);
    private AThread a;

    public BThread(AThread a) {
        this.a = a;
    }

    @Override
    public void run() {
        while (true){
            System.out.println("输入一个字符");
            String next = scanner.next();
            if ("Q".equals(next)){
                a.setLoop(false);
                break;
            }
        }
        System.out.println("B线程结束");
    }
}


class CThread implements Runnable{

    public static int money = 10000;
    public boolean loop = true;
    private Object object = new Object();

    public /*static*/ synchronized void sell(){
        if (money <1000){
            System.out.println("余额不足");
            loop = false;
            return;
        }
        money -= 1000;
        System.out.println(Thread.currentThread().getName()+"少了1000,现在剩余"+money);
    }
    public static   void sell1(){
      synchronized (CThread.class){
          if (money <1000){
              System.out.println("余额不足");
//              loop = false;
              return;
          }
          money -= 1000;
          System.out.println(Thread.currentThread().getName()+"少了1000,现在剩余"+money);
      }
    }
    @Override
    public    void run() {
        while (loop){
//            synchronized (this /* object */){
//                if (money <1000){
//                    System.out.println("余额不足");
//                    break;
//                }
//
//
//            money -= 1000;
//            System.out.println(Thread.currentThread().getName()+"少了1000,现在剩余"+money);
//            }
            sell();
//            sell1();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
